package com.example.weather_app.myadapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.weather_app.R;
import com.example.weather_app.bean.WeatherBean;
import com.example.weather_app.db.DatabaseBean;
import com.google.gson.Gson;

import java.util.List;

public class CityManagerAdapter extends BaseAdapter {
    private Context context;
    private List<DatabaseBean> databaseBeanList;

    public CityManagerAdapter(Context context, List<DatabaseBean> databaseBeanList) {
        this.context = context;
        this.databaseBeanList = databaseBeanList;
    }

    @Override
    public int getCount() {
        return databaseBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return databaseBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_city_manager,null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        DatabaseBean bean = databaseBeanList.get(position);
        holder.itemCityTvCity.setText(bean.getCity());
        WeatherBean weatherBean = new Gson().fromJson(bean.getContent(), WeatherBean.class);

        //获取今日天气情况
        String temperature = weatherBean.getResult().getRealtime().getTemperature();
        holder.itemCityTvTemp.setText(temperature + "℃");
        WeatherBean.ResultDTO.FutureDTO futureDTO = weatherBean.getResult().getFuture().get(0);
        holder.itemCityTvCondition.setText("天气："+futureDTO.getWeather());
        holder.itemCityTvWind.setText(futureDTO.getDirect());
        holder.itemCityTemprange.setText(futureDTO.getTemperature().replace("/","~"));

        return convertView;
    }

    class ViewHolder {
        private TextView itemCityTvCity, itemCityTvTemp, itemCityTvCondition, itemCityTvWind, itemCityTemprange;

        public ViewHolder(View itemView) {
            itemCityTvCity = (TextView) itemView.findViewById(R.id.item_city_tv_city);
            itemCityTvTemp = (TextView) itemView.findViewById(R.id.item_city_tv_temp);
            itemCityTvCondition = (TextView) itemView.findViewById(R.id.item_city_tv_condition);
            itemCityTvWind = (TextView) itemView.findViewById(R.id.item_city_tv_wind);
            itemCityTemprange = (TextView) itemView.findViewById(R.id.item_city_temprange);
        }
    }
}
